package com.db.patterns.singleton.demo10;

public class Singleton {
    //volatile 防止指令重排，出现空指针异常
    private static volatile Singleton instance;
    private static Object lock = new Object();

    private Singleton() {

    }

    public static Singleton getInstance() {
        // 先判断实例是否存在，不存在在在加锁处理
        if (instance == null) {
            synchronized (lock) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
